﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CompareRecord
{
    class myFunction
    {
        public void Run()
        {
            //String S1 = "Helo";
            //String S2 = "Helo";

            //if (S1 == S2)
            //{
            //    Console.WriteLine("Equal");
            //}
            //else
            //{
            //    Console.WriteLine("Not Equal");

            //}


            // 读取记录
            List<String> firList = GetList("1.txt");
            List<String> secList = GetList("2.txt");

            // 比较记录
            double dResult = Compare(firList, secList);


            // 输出结果
            Console.WriteLine(dResult);    
        }

        /// <summary>
        /// 比较两个List里面的内容
        /// </summary>
        /// <param name="firList"></param>
        /// <param name="secList"></param>
        /// <returns></returns>
        private double Compare( List<string> firList, 
                                List<string> secList)
        {
            double dRtn = 0.0;
            double count = 0.0;

            int nNum = firList.Count;
            // 遍历元素
            foreach (string eachitem in firList)
            {
                // 每次取出一个元素. eachitem

                bool bIsIn = IsIn(eachitem, secList);

                if (bIsIn)
                {
                    count = count + 1.0;
                }
            }

            dRtn = count / nNum;

            return dRtn;
        }

        /// <summary>
        /// 判断eachitem是否在secList里
        /// </summary>
        /// <param name="eachitem"></param>
        /// <param name="secList"></param>
        /// <returns></returns>
        private bool IsIn(string compareItem, List<string> secList)
        {
            bool bRtn = false;

            foreach (string eachitem in secList)
            {
                if (compareItem == eachitem)
                {
                    bRtn = true;
                    break;
                }
            }

            return bRtn;
        }

        public List<String> GetList(string Path)
        {
            List<string> shuList = new List<string>();

            StreamReader sr = new StreamReader(Path);

            String line;
            // Read and display lines from the file until the end of 
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
                // 加入书包.
                shuList.Add(line);

                //Console.WriteLine(line);
            }

            sr.Close();
            sr.Dispose();

            return shuList;
        }
    }
}
